using UnityEngine;
using System.Collections;
using System;
using System.IO;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Net;

public class gameManager : MonoBehaviour 
{
    public static float masterVol = 1.0f, musicVol = 1.0f, shadowes = 500 , mouseSenstivity = 15;
    public static bool softParticles = false;
    public static int textureQuality = 1, pixelLightCount = 2, antiAliasing = 2;
    public static Material[] camos;
    public static int Level = 60, xp = 21;

    private void Awake()
    {
        DontDestroyOnLoad(gameObject);

        try
        {
            TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\frontier\video.sav");
            masterVol = float.Parse(read.ReadLine());
            musicVol = float.Parse(read.ReadLine());
            shadowes = float.Parse(read.ReadLine());
            textureQuality = int.Parse(read.ReadLine());
            antiAliasing = int.Parse(read.ReadLine());
            if (read.ReadLine() == "True")
                softParticles = true;
            else
                softParticles = false;
            pixelLightCount = int.Parse(read.ReadLine());
            mouseSenstivity = float.Parse(read.ReadLine());
            read.Close();
        }
        catch (Exception e)
        {
            print(e);
            try
            {
                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\frontier\video.sav");
                write.WriteLine(masterVol.ToString());
                write.WriteLine(musicVol.ToString());
                write.WriteLine(shadowes.ToString());
                write.WriteLine(textureQuality.ToString());
                write.WriteLine(antiAliasing.ToString());
                write.WriteLine(softParticles.ToString());
                write.WriteLine(pixelLightCount.ToString());
                write.WriteLine(mouseSenstivity.ToString());
                write.Close();
            }
            catch(Exception)
            {
                if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\frontier\video.sav"))
                    File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\frontier\video.sav");

                DirectoryInfo di = Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\frontier\");
                di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
                saveSettings();
            }
        }
        InvokeRepeating("saveSettings", 1, 10);
    }

    public static void GetUserData()
    {
        string dataBaseConect = "Server=localhost;Database=zee595_unknown;User ID=zee595_frontier;Password=vacume;Pooling=true";
        MySqlConnection conn = new MySqlConnection(dataBaseConect);
        MySqlCommand command = new MySqlCommand();
        command.CommandText = "SELECT * FROM users WHERE user=" + login.user;
        try
        {
            conn.Open();
            MySqlDataReader mysqlReader = command.ExecuteReader();
            while(mysqlReader.Read())
            {
                print(mysqlReader["picUrl"].ToString());
            }
        }
        catch (Exception ex)
        {
            print("ERROR: " + ex);
        }
        finally
        {
            conn.Close();
        }
    }

    private void saveSettings()
    {
        TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\frontier\video.sav");
        write.WriteLine(masterVol.ToString());
        write.WriteLine(musicVol.ToString());
        write.WriteLine(shadowes.ToString());
        write.WriteLine(textureQuality.ToString());
        write.WriteLine(antiAliasing.ToString());
        write.WriteLine(softParticles.ToString());
        write.WriteLine(pixelLightCount.ToString());
        write.WriteLine(mouseSenstivity.ToString());
        write.Close();
    }

    private void Update()
    {
        AudioListener.volume = masterVol;
        QualitySettings.shadowDistance = shadowes;
        QualitySettings.masterTextureLimit = int.Parse(textureQuality.ToString());
        QualitySettings.anisotropicFiltering = AnisotropicFiltering.Enable;
        QualitySettings.antiAliasing = int.Parse(antiAliasing.ToString());
        QualitySettings.softVegetation = softParticles;
        QualitySettings.pixelLightCount = int.Parse(pixelLightCount.ToString());
    }
}
